package com.kun.lagou.util;

/**
 * @author Administrator
 */
public class Base62Util {
    /**
     * 初始化 62 进制数据，索引位置代表转换字符的数值 0-61，比如 A代表10，z代表61
     */
    private static String BASE62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

    public static String idToString(long id) {
        StringBuilder stringBuilder = new StringBuilder();
        while(id > 0) {
            stringBuilder.append(BASE62.charAt((int) (id % 62)));
            id = id / 62;
        }
        while(stringBuilder.length() < 6) {
            stringBuilder.append(0);
        }
        return stringBuilder.reverse().toString();
    }

    public static long stringToId(String shortKey) {
        int id = 0;
        for(int i = 0;i<shortKey.length();i++) {
            id = id * 62 + BASE62.indexOf(shortKey.charAt(i));
        }
        return id;
    }
}
